package com.xzx.study;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class TestHandlerActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = TestHandlerActivity.class.getSimpleName();

    private ProgressBar test_handler_progress_bar;
    private Button test_handler_submit1;
    private Button test_handler_submit2;
    private TextView test_handler_text;

    // 主线程中执行
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                test_handler_progress_bar.setVisibility(View.INVISIBLE);
                test_handler_text.setText(msg.obj.toString());
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_handler);

        test_handler_progress_bar = findViewById(R.id.test_handler_progress_bar);
        test_handler_submit1 = findViewById(R.id.test_handler_submit1);
        test_handler_submit2 = findViewById(R.id.test_handler_submit2);
        test_handler_text = findViewById(R.id.test_handler_text);

        test_handler_submit1.setOnClickListener(this);
        test_handler_submit2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == test_handler_submit1) {
            handle1();
        } else if (v == test_handler_submit2) {
            handle2();
        }
    }

    private void handle2() {
        Toast.makeText(this, "submit2", Toast.LENGTH_SHORT).show();
        test_handler_progress_bar.setVisibility(View.VISIBLE);
        new Thread(() -> {
            try {
                URL url = new URL("http://192.168.31.60:8182/android/test");
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(50000);
                connection.setReadTimeout(50000);
                connection.connect();

                final StringBuilder builder = new StringBuilder();
                int responseCode = connection.getResponseCode();
                if (responseCode == 200) {
                    InputStream inputStream = connection.getInputStream();
                    byte[] buffer = new byte[1024 * 1000];
                    int len;
                    while ((len = inputStream.read(buffer)) > 0) {
                        builder.append(new String(buffer, 0, len));
                    }
                    inputStream.close();
                }
                connection.disconnect();
                Message message = Message.obtain();
                message.what = 1;
                message.obj = builder.toString();
                handler.sendMessage(message);
            } catch (Exception e) {
                Log.e(TAG, "网络请求异常", e);
            }
        }).start();
    }

    private void handle1() {
        Toast.makeText(this, "submit1", Toast.LENGTH_SHORT).show();
        test_handler_progress_bar.setVisibility(View.VISIBLE);
        new Thread(() -> {
            try {
                URL url = new URL("http://192.168.31.60:8182/android/test");
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(50000);
                connection.setReadTimeout(50000);
                connection.connect();

                final StringBuilder builder = new StringBuilder();
                int responseCode = connection.getResponseCode();
                if (responseCode == 200) {
                    InputStream inputStream = connection.getInputStream();
                    byte[] buffer = new byte[1024 * 1000];
                    int len;
                    while ((len = inputStream.read(buffer)) > 0) {
                        builder.append(new String(buffer, 0, len));
                    }
                    inputStream.close();
                }
                connection.disconnect();
                runOnUiThread(() -> {

                    test_handler_progress_bar.setVisibility(View.INVISIBLE);
                    test_handler_text.setText(builder.toString());
                });
            } catch (Exception e) {
                Log.e(TAG, "网络请求异常", e);
            }
        }).start();

    }
}
